var location = require('../../utils/LocationUtil.js')
var LogUtil = require('../../utils/LogUtil.js')
var http = require('../../utils/httpUtil.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-09-15',
    },
    // 现场照片与收集资料相关
    siteImages: 200,
    fontColor: '#FFC125',
    modalHidden: false,
    name: "",
    age: 0,
    //损失明细
    lossName: [],
    dinsun: true,
    array: ["未选择", "无法确实", "是", "否"],
    baodanIndex: '未选择',
    address: '没获取到地理信息',
    isRescue: true,
    isNvite: true,
    isPositive: true,
    salvage: true,
    commentary: true,
    surveyDes: '',
    sanfang: true,
    daode: true,
    anjian: "",
    SurveyTaskId: "",
    SurveyId: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.anjian = options.anjian
    this.data.SurveyTaskId = options.SurveyTaskId
    this.initialSurvey()
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.setNavigationBarTitle({
      title: "项目"
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var losses = this.data.lossName
    var images = wx.getStorageSync('images')
    this.setData({
      lossName: losses,
      siteImages: images.length
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {


  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  textClick: function() {
    this.setData({
      fontColor: '#1C86EE'
    })
    console.log("点击模版里的按钮，并调用逻辑的方法")
    wx.clearStorage()
    wx.getStorage({
      key: 'key',
      success(res) {
        console.log(res.data)
      }
    })
    location.searchLocation('保利百合', function(res) {
      console.log(res)
    })
  },
  nameFun: function(event) {
    this.setData({
      name: event.detail.value
    })
    console.log(event.detail)
  },
  ageFun: function(event) {
    this.setData({
      age: event.detail.value
    })
  },
  modalBindaconfirm: function() {
    this.setData({
      modalHidden: true
    })
  },
  modalBindcancel: function() {
    this.setData({
      modalHidden: true
    })
  },
  // 是否需要施救
  rescueYes() {
    this.setData({
      isRescue: true
    })
  },
  rescueNo() {
    this.setData({
      isRescue: false
    })
  },
  // 是否发施救涵
  inviteYes() {
    this.setData({
      isNvite: true
    })
  },
  inviteNo() {
    this.setData({
      isNvite: false
    })
  },
  // 施救是否积极
  positiveYes() {
    isPositive: true
  },
  positiveNo() {
    isPositive: false
  },
  // 能否现场定损
  dinYes: function() {
    this.setData({
      dinsun: true
    })
  },
  dinNo: function() {
    this.setData({
      dinsun: false
    })
  },
  daodeY: function() {
    this.data.daode = true;
  },
  daodeN: function() {
    this.data.daode = false;
  },
  salvageY: function() {
    this.data.salvage = true
  },
  salvageN: function() {
    this.data.salvage = false
  },
  commentaryY: function() {
    this.data.commentary = true
  },
  commentaryN: function() {
    this.data.commentary = true
  },
  sanfangY: function() {
    this.data.sanfang = true
  },
  sanfangN: function() {
    this.data.sanfang = false
  },
  surveyMessage: function(event) {
    console.log("输入的值为", event.detail.value)
    this.data.surveyDes = event.detail.value;
  },
  // 修改损失信息的跳转
  updateLoss: function(loss1) {
    wx.navigateTo({
      url: '/pages/addlose/addloseMessage?lossObj=' + JSON.stringify(loss1.currentTarget.dataset.loss) + '&index=' + loss1.currentTarget.dataset.index
    })
  },
  // 添加损失信息
  addloss: function() {
    console.log(this.data.lossName)
    wx.navigateTo({
      url: '/pages/addlose/addloseMessage',
    })
  },
  // 删除损失信息
  deleteLoss(loss1) {
    console.log("删除损失信息")
    var lossNames = this.data.lossName
    lossNames.splice(loss1.currentTarget.dataset.index, 1)
    this.setData({
      lossName: lossNames
    })
  },
  // 普通选择器
  bindPickerChange: function(event) {
    this.setData({
      baodanIndex: event.detail.value
    })
  },
  // 跳到拍照页
  takePhoto: function() {
    wx.navigateTo({
      url: '/pages/photo/photo',
    })
  },
  getLocation: function(res) {
    var that = this
    location.getLocationDate(function(res) {
      let province = res.result.ad_info.province
      let city = res.result.ad_info.city
      that.setData({
        address: province + city
      })
    })
  },
  //保存数据
  saveData: function() {
    var record = {}
    record.address = this.data.address;
    record.isRescue = this.data.isRescue;
    record.isNvite = this.data.isNvite;
    record.isPositive = this.data.isPositive;
    record.salvage = this.data.salvage;
    record.commentary = this.data.commentary;
    record.surveyMessage = this.data.surveyDes;
    record.dinsun = this.data.dinsun;
    record.baodan = this.data.baodanIndex;
    record.daode = this.data.daode;
    record.sanfang = this.data.sanfang;
    record.loss = this.data.lossName;
    wx.setStorageSync("lossData", record)
    wx.showToast({
      title: '成功',
    })
  },
  nextStep: function() {
    this.saveData()
    wx.navigateTo({
      url: '/mulu1/four/fourPage',
    })
  },
  getData: function() {
    var that = this;
    var param = {}
    param.ReportId = this.data.anjian
    param.SurveyTaskId = this.data.SurveyTaskId
    param.SourceId = 1
    param.SurveyId = this.data.SurveyId
    http.PostHttp("Survey/GetAppSurvey", param, function(res) {
      if (res.Code == 1) {
        that.setData({
          address: res.Data.SurveyDetail.CheckAddress.length == 0 ? that.location() : res.Data.SurveyDetail.CheckAddress,
          isRescue: res.Data.SurveyDetail.IsNeedRescue == 1 ? true : false,
          isNvite: res.Data.SurveyDetail.IsRescueed == 1 ? true : false,
          isPositive: res.Data.SurveyDetail.IsPositiveResidual == 1 ? true : false,
          salvage: res.Data.SurveyDetail.IsNeedResidualDeal == 1 ? true : false,
          commentary: res.Data.SurveyDetail.IsVideoCommentared == 1 ? true : false,
          surveyDes: res.Data.SurveyDetail.LossDescription, //查勘详情
          sanfang: res.Data.ReportAnalysis.IsAnotherResponsibility == 1 ? true : false,
          daode: res.Data.ReportAnalysis.IsMoralRisk == 1 ? true : false,
          dinsun: res.Data.SurveyDetail.IsSpotDetermineLoss == 1 ? true : false,
          baodanIndex: res.Data.ReportAnalysis.IsInsuranceResponsibility,
          lossName: res.Data.SurveyLoss.SurveyLossList
        })
      } else {
        wx.showToast({
          title: res.Message,
        })
      }
    })
  },
  initialSurvey: function() {
    var that = this;
    var param = {}
    param.ReportId = this.data.anjian
    param.SurveyTaskId = this.data.SurveyTaskId
    param.SourceId = 1
    http.PostHttp("Survey/InitialSurvey", param, function(res) {
      if (res.Code == 1) {
        that.data.SurveyId = res.Data.Id
        that.getData()
      } else {
        wx.showToast({
          title: res.Message,
        })
      }
    })
  }
})